package com.hxzy.wx.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.hxzy.common.exception.HxdsException;
import com.hxzy.common.util.R;
import com.hxzy.drvier.api.DriverOpenFeignApi;
import com.hxzy.drvier.req.CreateDriverFaceModelReq;
import com.hxzy.drvier.req.RegisterNewDriverReq;
import com.hxzy.drvier.req.UpdateDriverAuthReq;
import com.hxzy.drvier.resp.DriverResp;
import com.hxzy.wx.service.DriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/12/14 14:16
 * @Version 1.0
 */
@Service
public class DriverServiceImpl implements DriverService {
    /**
     * 后台司机服务API
     */
    @Autowired
    private DriverOpenFeignApi driverOpenFeignApi;


    /**
     * 分布式事务的开始
     * @param req
     * @return
     */
    @LcnTransaction
    @Transactional
    @Override
    public String registerNewDriver(RegisterNewDriverReq req) {
        R<Map<String, String>> mapR = this.driverOpenFeignApi.registerNewDriver(req);
        if(mapR.getCode()==200){
            String userId=mapR.getData().get("userId");
            //登录成功
            long uid=Long.parseLong(userId);
            StpUtil.login(uid);

            //获取令牌
            return  StpUtil.getTokenValue();


        }else{
           throw new HxdsException(mapR.getMsg());
        }
    }

    /**
     * 远程拉取数据信息
     * @param userId
     * @return
     */
    @Override
    public DriverResp getDriverInfo(long userId) {
        R<DriverResp> r = this.driverOpenFeignApi.getDriverInfo(userId);
        return r.getData();
    }

    @LcnTransaction
    @Transactional
    @Override
    public int updateDriverAuth(UpdateDriverAuthReq req) {
        R<Integer> r= this.driverOpenFeignApi.updateDriverAuth(req);
        return r.getData();
    }

    /**
     * 调用远程司机端服务,归脸人脸信息
     * @param req
     * @return
     */
    @LcnTransaction
    @Transactional
    @Override
    public R<Map<String, String>>  createDriverFaceModel(CreateDriverFaceModelReq req) {
        R<Map<String, String>> driverFaceModel = this.driverOpenFeignApi.createDriverFaceModel(req);
        return driverFaceModel;
    }
}
